using UnityEngine;
using System.Collections;
using DanielSig;
using DanielSig.UnityExtensions;

public class ScreenFlasher : MonoBehaviour
{
	public string ScreenFlashName;
	public float FlashTime;
	
	[Range(0,1)]
	public float StartAlpha;
	
	
	private Color _normalColor;
	private Color _currentColor;
	private float _countDown;
	private GUITexture _screenFlash;
	
	void Start()
	{
		_screenFlash = GameObject.Find(ScreenFlashName).guiTexture;
		_normalColor = _screenFlash.color;
	}
	
	void FlashScreen(Color color)
	{
		_screenFlash.color = _currentColor = color.SetAlpha(StartAlpha);
		_countDown = FlashTime;
	}
	void FlashScreen(Vector3 color)
	{
		_screenFlash.color = _currentColor = new Color(color.x, color.y, color.z, StartAlpha);
		_countDown = FlashTime;
	}
	void Update()
	{
		if(_countDown > 0)
		{
			_countDown -= Time.deltaTime;
			_screenFlash.color = Color.Lerp(_normalColor, _currentColor, _countDown / FlashTime);
		}
	}
	void OnDestroy()
	{
		if(_screenFlash) _screenFlash.color = _normalColor;
	}
}

